﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Grain.Crm.DataMigrator.CrmServices;

namespace Grain.Crm.DataMigrator.Imports.EntityImports
{
    public class ContactSkillImport:ImportBase
    {
        public ContactSkillImport(CrmService service, WhoAmIResponse whoAmIresponse, ImportPayload importPayload)
            : base(service, whoAmIresponse, importPayload)
        {
        }

        protected override void Preprocess(out bool entityExists)
        {
            HeaderRow += ", Contact, Curriculum Vitae, Language";
            FileContent += "," + ImportsGlobal.ContactId + "," + ImportsGlobal.CV + "," + ImportsGlobal.Language;

            var skillKeyId = ImportsHelper.GetEntityId(_service, EntityName.new_skill.ToString(), new string[] { "new_name" }, new string[] { FileContent.GetByHeading(HeaderRow, "Skill") });
            var contactSkillId = ImportsHelper.GetEntityId(_service, ThisEntityName, new string[] { "new_contactid", "new_curriculumvitaeid", "new_skillid" },
                new string[] { ImportsGlobal.ContactId, ImportsGlobal.CVId, skillKeyId.ToString() });

            if (skillKeyId == null)
                throw new InvalidOperationException("Could not find skill in Crm:" + FileContent.GetByHeading(HeaderRow, "Skill"));

            if (contactSkillId != null)
            {
                entityExists = true;

                ImportsHelper.AddKeyValueToContent(contactSkillId.GetValueOrDefault(), ThisEntityName, ref HeaderRow, ref FileContent);
            }
            else
                entityExists = false;


        }
    }
}
